package com.xbdun.web.bmp.controller.base;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.xbdun.common.api.XbdUnResult;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.upload.request.UploadByBASE64Request;
import com.xbdun.common.upload.request.UploadByBASE64SubRequest;
import com.xbdun.comweb.bmp.session.CurrentUserInfo;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.comweb.bmp.util.WebCommonUtil;
import com.xbdun.facade.validatebmp.constant.DubboValidateBmpConst;
import com.xbdun.facade.validatebmp.service.upload.OpenUploadFacade;

import lombok.extern.slf4j.Slf4j;

/**
* @ClassName: UploadFileController
* @Description: 本地上传文件服务
* @author zhanggt 360730440@qq.com
* @date 2017年5月17日 下午4:24:34
*/
@Slf4j
@Component(UploadFileController.UPLOAD_URL)
public class UploadFileController extends AbstractController{
	//文件上传地址 
	public final static String UPLOAD_URL = "/_native_/uploadfile";
	// 文件上传
	@Reference(registry=DubboValidateBmpConst.REGISTRYS)
	OpenUploadFacade openUploadFacade;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)throws Exception {
		MultipartHttpServletRequest multpartRequest = null;
		Map<String,MultipartFile> fileMap= null;
		UploadByBASE64Request requestx = null;
		CurrentUserInfo currentUserInfo = UserInfoUtil.getCurrentUserInfo(request);
		//上传文件类型
		String uploadType = request.getParameter("uploadType");
		//希望返回的图片大小类型
		String upfmt = request.getParameter("upfmt");
		if(request instanceof MultipartHttpServletRequest){
			multpartRequest = ((MultipartHttpServletRequest)request);
		}
		//合法参数校验
		if(null==openUploadFacade||null==multpartRequest||!NumberUtils.isDigits(uploadType)){
			XbdUnStringResult result = new XbdUnStringResult();
			result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("%s上传参数校验失败，不是合法参数[%s]",(null==openUploadFacade?"对不起系统未提供上传服务":""),uploadType));
			WebCommonUtil.outWrite(response, JSON.toJSONString(result));
			return null;
		}
		
		//文件类型判断
		fileMap = multpartRequest.getFileMap();
		Iterator<String> keyIt=fileMap.keySet().iterator();
		requestx = new UploadByBASE64Request();
		requestx.setFileBase64(new UploadByBASE64SubRequest());
		requestx.setUploadType(Integer.valueOf(uploadType));
		
		//设置为当前机构的机构编号
		requestx.setOrgCode(null!=currentUserInfo?currentUserInfo.getOrgCode()+"":"100213");
		requestx.setReturnFormat(upfmt);//希望返回图片格式
		while(keyIt.hasNext()){
			String key=keyIt.next();
			MultipartFile file=fileMap.get(key);
			try {
				requestx.getFileBase64().setExtName(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),file.getOriginalFilename().length()));
				requestx.getFileBase64().setFile(Base64Utils.encodeToString(file.getBytes()));
				XbdUnResult<?> uploadByBASE64 = openUploadFacade.uploadByBASE64(requestx);
				WebCommonUtil.outWrite(response, JSON.toJSONString(uploadByBASE64));
			} catch (IOException e) {
				log.error("上传文件异常，上传类型【"+uploadType+"】",e);
				XbdUnStringResult result = new XbdUnStringResult();
				result.setBizException(BizException.PARAM_CHKE_FAIL.newInstance("上传参数校验异常，不是合法参数[%s]", uploadType));
				WebCommonUtil.outWrite(response, JSON.toJSONString(result));
			}
		}
		return null;
	}
 	
}
